<?php
require_once 'func.php';
$path = '/upload/nicedit/'.date('ymd').'/';
$name = uniqid(rand(1,99999)).rand(1,999);
$exts = array('gif','jpg','jpeg','png', 'jpe');
$mimes = array(	'gif'	=>	'image/gif',
				'jpeg'	=>	array('image/jpeg', 'image/pjpeg'),
				'jpg'	=>	array('image/jpeg', 'image/pjpeg'),
				'jpe'	=>	array('image/jpeg', 'image/pjpeg'),
				'png'	=>	array('image/png',  'image/x-png'),
			);
$size = 1;
try
{
	
	unwrap_sign($_POST);
	if( !in_array($_POST['host'], array('www.ppuying.dev','www.ppuying.com')))
	{
		throw new Exception("Access denied..");
	}

	if(isset($_FILES['file']['tmp_name']))
	{
		/**
		 * Upload file size check.
		 */
		if($_FILES['file']['size'] > $size * 1024 * 1024)
		{
			throw new Exception("Upload file size limit.. File is too big. Try to uplaod a file below {$size} Mbyte..");
		}

		/**
		 * Upload file extension check.
		 */
		$file_name = $_FILES['file']['name'];
		$file_type = $_FILES['file']['type'];
		$info = pathinfo($file_name);
		if(! in_array(strtolower($info['extension']), $exts))
		{
			throw new Exception("Only ".implode(", ", $exts)." files can be uploaded..[{$info['extension']}]");
		}

		/**
		 * Upload file type check.
		 */
		$mime = (array) $mimes[strtolower($info['extension'])];
		if(in_array($file_type, $mime) == false)
		{
			throw new Exception("Only image type file can be uploaded.[{$file_type}].");
		}

		/**
		 * send a file through ftp
		 */
		if(is_dir($_SERVER['DOCUMENT_ROOT'].$path) == false)
		{
			@mkdir($_SERVER['DOCUMENT_ROOT'].$path);
		}
		if(move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].$path.$name))
		{
			$param = array(	'url'	=>	"http://".str_replace("www.", "file.", $_POST['host'])."/".$path.$name,
							'name'	=>	$file_name
							);
			wrap_sign($param);
			header("Location: http://{$_POST['host']}/plugin/uploader/callback.php?".http_build_query($param));
		}
		else
		{
			throw new Exception($_SERVER['DOCUMENT_ROOT'].$path.$name);
		}
	}
}
catch (Exception $e)
{
	exit("<script>alert('{$e->getMessage()}');</script>");
}